package com.initial.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.initial.entity.TbCrmEmployee;
import com.initial.mapper.TbCrmEmployeeMapper;
import com.initial.service.TbCrmEmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


@Controller
public class EmployeeController {
    @Resource
    public TbCrmEmployeeMapper employeeMapper;
    @Resource
    private TbCrmEmployeeService employeeService;

//    跳转员工展示页面
    @RequestMapping("/toEmployee")
    public String skips(){
        return "views/manage/employee/list_employee";
    }

//    获取分页数据
    @RequestMapping("/listEmployee")
    @ResponseBody
    public Map list(Integer page, Integer rows,String name){
        Map<String,Object> map = new HashMap<>();
//        声明局部变量"tbCrmEmployeeIPage"
        IPage<TbCrmEmployee> tbCrmEmployeeIPage;

//        模糊查询拼接条件
        QueryWrapper<TbCrmEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);

        //        分页方法调用
        if(name==null){
            tbCrmEmployeeIPage = employeeMapper.selectPage(new Page<>(page, rows),null);
        }else{
            tbCrmEmployeeIPage = employeeMapper.selectPage(new Page<>(page, rows),queryWrapper);
        }
//        前台显示数据总条数
        map.put("total",tbCrmEmployeeIPage.getTotal());
//        每一页显示的数据量
        map.put("rows",tbCrmEmployeeIPage.getRecords());
        System.out.println(map);
        return map;
    }


//    跳转添加员工页面
    @RequestMapping("/toAddEmployee")
    public String skipAdd(){
        System.out.println("跳转");
        return "views/manage/employee/add_employee";
    }

//    添加功能
    @RequestMapping("/AddEmployee")
    @ResponseBody
    public ModelMap addEmployee(TbCrmEmployee employee){
//        new一个新的ModelMap对象用来存取提示信息
        ModelMap map = new ModelMap();
//        利用接口方法执行数据插入
        boolean save = employeeService.save(employee);
//        根据插入方法返回结果，添加不同的提示信息
        if(save){
            map.addAttribute("success",true);
            map.addAttribute("msg","添加成功！");
        }else{
            map.addAttribute("success",false);
            map.addAttribute("msg","添加失败,注意规范！");
        }
        return map;
    }

//    删除功能,可以批量删除
    @RequestMapping("/toDeleteEmployee")
    @ResponseBody
    public Map<String,Object> deleteEmployee(String[] ids){
        Map<String,Object> map = new HashMap<>();
        boolean b = employeeService.removeByIds(Arrays.asList(ids));
        if (b){
            map.put("success",true);
            map.put("message","删除成 功");
        }else {
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }

//    修改功能1/2
    @RequestMapping("/toEditEmployee")
    public String toEditEmployee(Integer id, HttpServletRequest req){
//        根据id查出来需要修改的员工信息
        TbCrmEmployee tbCrmEmployee = employeeMapper.selectById(id);
//        将员工信息放入"request"中
        req.setAttribute("editEmployee",tbCrmEmployee);
//        用转发的方式将数据带到前端页面
        return "/views/manage/employee/edit_employee";
    }

    //    修改功能1/2
    @RequestMapping("/EditEmployee")
    @ResponseBody
    public ModelMap editEmployee(TbCrmEmployee employee){
//        new一个新的ModelMap对象用来存取提示信息
        ModelMap map = new ModelMap();
//        利用service方法执行数据修改
        int i = employeeMapper.updateById(employee);
//        根据插入方法返回结果，添加不同的提示信息
        if(i>0){
            map.addAttribute("success",true);
            map.addAttribute("message","修改成功！");
        }else{
            map.addAttribute("success",false);
            map.addAttribute("message","修改失败,注意规范！");
        }
        return map;
    }

}

